package cn.xh.org.service.impl;

import cn.xh.basic.service.impl.BaseServiceImpl;
import cn.xh.basic.util.AjaxResult;
import cn.xh.basic.util.BusinessException;
import cn.xh.org.domain.Employee;
import cn.xh.org.domain.Shop;
import cn.xh.org.mapper.EmployeeMapper;
import cn.xh.org.mapper.ShopMapper;
import cn.xh.org.service.IEmployeeService;
import cn.xh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    /*
    * extends BaseServiceImpl<Employee>
    *     1.实现基础接口中的方法
    * */

    //2.实现扩展接口

    @Autowired
    private ShopMapper employeeMapper;

    public AjaxResult settleIn(@RequestBody Shop shop) {
        try {
            // 先要通过店铺获取到管理员
            Employee admin = shop.getAdmin();
            // 再把对象放入数据库中查询,email,phone,username中只要有一个存在就说明之前入住过
            Employee employee = employeeMapper.loadByUsername(admin);
            // 判断管理员是否已入住
            if(employee != null) { // 说明商家已经入住
                throw new BusinessException("该用户已经注册过店铺，请直接登录！");
            }
            // 先保存员工信息

            shopService.add(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
}
